﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using MavlinkDebugger.PacketViewer;
using MavlinkPacketViewer.PacketViewer;

namespace SLPropertyGrid
{
	public class DateTimeValueEditor : ValueEditorBase
	{
		#region Fields
		object currentValue;
		bool showingDTP;
		StackPanel pnl;
		protected TextBox txt;
		protected DatePicker dtp;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="label"></param>
		/// <param name="property"></param>
		public DateTimeValueEditor(PropertyGridLabel label, PropertyItem property)
			: base(label, property)
		{
			currentValue = property.Value;

			pnl = new StackPanel();
			this.Content = pnl;

			dtp = new DatePicker();
			dtp.Visibility = Visibility.Visible;
            dtp.Margin = new Thickness(0);
            dtp.Padding = new Thickness(2);
			dtp.VerticalAlignment = VerticalAlignment.Center;
			dtp.HorizontalAlignment = HorizontalAlignment.Stretch;
			dtp.CalendarOpened += dtp_CalendarOpened;
			dtp.CalendarClosed += dtp_CalendarClosed;
            dtp.Background = new SolidColorBrush(Colors.White);
            dtp.Foreground = this.Property.CanWrite 
                ? new SolidColorBrush(Colors.Black) 
                : new SolidColorBrush(Colors.Gray);

			pnl.Children.Add(dtp);
			dtp.Focus();

			this.ShowTextBox();
		}
		#endregion

		#region Overrides
		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnGotFocus(RoutedEventArgs e)
		{
			Debug.WriteLine("DateTimeValueEditor : OnGotFocus");

			if (showingDTP)
				return;

			base.OnGotFocus(e);

			if (this.Property.CanWrite)
				this.ShowDatePicker();
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnLostFocus(RoutedEventArgs e)
		{
			Debug.WriteLine("DateTimeValueEditor : OnLostFocus");

			if (showingDTP)
				return;

			base.OnLostFocus(e);
		}
		#endregion

		#region Methods
		void ShowDatePicker()
		{
			if (null == txt)
				return;

			dtp.Visibility = Visibility.Visible;
			dtp.Focus();

			txt.Visibility = Visibility.Collapsed;
			pnl.Children.Remove(txt);
			txt = null;

			dtp.SelectedDate = (DateTime)currentValue;

		}
		void ShowTextBox()
		{
			if (null != txt)
				return;

			txt = new TextBox();
			txt.Height = 20;
			txt.BorderThickness = new Thickness(0);
			txt.Margin = new Thickness(0);
            txt.Padding = new Thickness(2);
            txt.VerticalAlignment = VerticalAlignment.Center;
			txt.HorizontalAlignment = HorizontalAlignment.Stretch;
			txt.Text = currentValue.ToString();
			txt.IsReadOnly = !this.Property.CanWrite;
			txt.Foreground = this.Property.CanWrite ? new SolidColorBrush(Colors.Black) : new SolidColorBrush(Colors.Gray);
            txt.Background = new SolidColorBrush(Colors.White);
            txt.Text = ((DateTime)this.Property.Value).ToShortDateString();
			pnl.Children.Add(txt);

			showingDTP = false;
			dtp.Visibility = Visibility.Collapsed;
		}
		#endregion

		void dtp_CalendarOpened(object sender, RoutedEventArgs e)
		{
			showingDTP = true;
		}

		void dtp_CalendarClosed(object sender, RoutedEventArgs e)
		{
			dtp.Focus();
		}

		
	}
}
